package com.escape.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.escape.subject.common.entity.PageResult;
import com.escape.subject.common.enums.IsDeletedEnum;
import com.escape.subject.domain.convert.SubjectInfoConvert;
import com.escape.subject.domain.entity.SubjectInfoBO;
import com.escape.subject.domain.entity.SubjectOptionBO;
import com.escape.subject.domain.handler.subject.SubjectTypeHandler;
import com.escape.subject.domain.handler.subject.SubjectTypeHandlerFactory;
import com.escape.subject.domain.service.SubjectInfoDomainService;
import com.escape.subject.infra.basic.entity.SubjectInfo;
import com.escape.subject.infra.basic.entity.SubjectLabel;
import com.escape.subject.infra.basic.entity.SubjectMapping;
import com.escape.subject.infra.basic.service.SubjectInfoService;
import com.escape.subject.infra.basic.service.SubjectLabelService;
import com.escape.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: escape_zebra
 * @CreateTime: 2024-12-26  10:52
 * @Description: TODO
 * @Version: 1.0
 */
@Service
@Slf4j
public class SubjectInfoDomainServiceImpl implements SubjectInfoDomainService {

    @Resource
    private SubjectInfoService subjectInfoService;
    @Resource
    private SubjectTypeHandlerFactory subjectTypeHandlerFactory;
    @Resource
    private SubjectMappingService subjectMappingService;
    @Resource
    private SubjectLabelService subjectLabelService;

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        if (log.isInfoEnabled()){
            log.info("SubjectInfoDomainServiceImpl.add.BO:{}", subjectInfoBO);
        }

        // 1. BO -> entity
        SubjectInfo subjectInfo = SubjectInfoConvert.INSTANCE.convertBoToInfo(subjectInfoBO);
        subjectInfo.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());

        // 2. 插入题目信息
        subjectInfoService.insert(subjectInfo);

        // 3. 获取题目类型工厂类
        SubjectTypeHandler handler = subjectTypeHandlerFactory.getHandler(subjectInfo.getSubjectType());
        subjectInfoBO.setId(subjectInfo.getId());

        // 4. 实现工厂类的添加方法
        handler.add(subjectInfoBO);

        // 5. 向映射表添加对应数据
        List<Integer> labelIds = subjectInfoBO.getLabelIds();
        List<Integer> categoryIds = subjectInfoBO.getCategoryIds();
        List<SubjectMapping> subjectMappingList = new LinkedList<SubjectMapping>();
        categoryIds.forEach(categoryId -> {
            labelIds.forEach(labelId -> {
                SubjectMapping subjectMapping = new SubjectMapping();
                subjectMapping.setSubjectId(subjectInfo.getId());
                subjectMapping.setLabelId(Long.valueOf(labelId));
                subjectMapping.setCategoryId(Long.valueOf(categoryId));
                subjectMapping.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
                subjectMappingList.add(subjectMapping);
            });
        });

        // 6. 插入映射集合
        subjectMappingService.batchInsert(subjectMappingList);
    }

    /**
     * 查询题目列表
     * @param subjectInfoBO
     * @return
     */
    @Override
    public PageResult<SubjectInfoBO> querySubjectInfo(SubjectInfoBO subjectInfoBO) {
        if (log.isInfoEnabled()){
            log.info("SubjectInfoDomainServiceImpl.add.BO:{}", subjectInfoBO);
        }

        // 1. 创建分页类
        PageResult<SubjectInfoBO> pageResult = new PageResult<>();
        pageResult.setPageNo(subjectInfoBO.getPageNo());
        pageResult.setPageSize(subjectInfoBO.getPageSize());

        // 2. 计算 start
        int start = (subjectInfoBO.getPageNo() - 1) * subjectInfoBO.getPageSize();

        // 3. 转换
        SubjectInfo subjectInfo = SubjectInfoConvert.INSTANCE.convertBoToInfo(subjectInfoBO);

        // 4. 查询总条数
        int count = subjectInfoService.countByCondition(
                subjectInfo,
                subjectInfoBO.getCategoryId(),
                subjectInfoBO.getLabelId());

        if (count == 0) {
            return pageResult;
        }

        // 5. 分页查询题目
        List<SubjectInfo> subjectInfoList =  subjectInfoService.querySubjectInfo(
                subjectInfo,
                subjectInfoBO.getCategoryId(),
                subjectInfoBO.getLabelId(),
                start,
                subjectInfoBO.getPageSize()
        );

        // 6. 转换
        List<SubjectInfoBO> boList = SubjectInfoConvert.INSTANCE.convertInfoListToBoList(subjectInfoList);
        pageResult.setRecords(boList);
        pageResult.setTotal(count);
        return pageResult;
    }

    /**
     * 查询题目详情
     * @param subjectInfoBO
     * @return
     */
    @Override
    public SubjectInfoBO queryDetail(SubjectInfoBO subjectInfoBO) {
        if (log.isInfoEnabled()){
            log.info("SubjectInfoDomainServiceImpl.add.BO:{}", subjectInfoBO);
        }

        // 1. 查询题目
        SubjectInfo subjectInfo = subjectInfoService.queryById(subjectInfoBO.getId());

        // 2. 查询答案：获取工厂方法
        SubjectTypeHandler handler = subjectTypeHandlerFactory.getHandler(subjectInfo.getSubjectType());
        SubjectOptionBO subjectOptionBO = handler.query(subjectInfoBO);

        // 3. 转换 entity --> bo
        SubjectInfoBO subjectInfoBoResult = SubjectInfoConvert.INSTANCE.convertOptionAndInfoToBo(subjectOptionBO, subjectInfo);

        // 4. 查询 labelIdList
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setSubjectId(subjectInfo.getId());
        subjectMapping.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
        List<SubjectMapping> subjectMappings = subjectMappingService.queryLabelIds(subjectMapping);
        List<Long> labelIdList = subjectMappings.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());

        // 5. 查询 labelNameList
        List<SubjectLabel> labelList = subjectLabelService.queryLabelByIds(labelIdList);
        List<String> labelNameList = labelList.stream().map(SubjectLabel::getLabelName).collect(Collectors.toList());

        // 6. 封装
        subjectInfoBoResult.setLabelName(labelNameList);

        return subjectInfoBoResult;
    }
}
